package com.finalBlog.admin.controller;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;

import com.finalBlog.annotation.Action;
import com.finalBlog.common.AdminBaseController;
import com.finalBlog.common.Dic;
import com.finalBlog.model.Adminuser;
import com.finalBlog.util.CommonTools;
import com.finalBlog.util.CookieTool;
import com.finalBlog.validator.LoginValidator;
import com.jfinal.aop.Before;

/**
 * finalBlog
 *	
 * 类/接口信息描述:
 *
 * @author dengxc
 *
 */
@Action(url="/admin/login")
public class LoginController extends AdminBaseController{
	private String username;
	private String password;
	private String remember;
	public void index(){
		render("admin/login.html");
	}
	public void captcha(){
		renderCaptcha();
	}
	@Before(LoginValidator.class)
	public void validate(){
		Adminuser user = Adminuser.dao.findByUserName(username);
		if(user==null){
			putMsg(Dic.login_err, false, 0,null);
			renderJson(jsonMsg);
			return;
		}
		String pwd = user.getPasswd();
		if(!password.equals(pwd)){
			putMsg(Dic.login_err, false, 0,null);
			renderJson(jsonMsg);
			return;
		}
		String ip =  CommonTools.getIP(getRequest());
		String time = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
		user.setLoginip(ip);
		user.setLogintime(time);
		user.update();
		if(StringUtils.isNotEmpty(remember)&&remember.equals("1")){
			CookieTool.addUsercookie(getRequest(), getResponse(), user, true);
		}else{
			CookieTool.addUsercookie(getRequest(), getResponse(), user, false);
		}
		putMsg("登陆成功跳转到主页", true, 0,"/admin/blog");
		renderJson(jsonMsg);
		return;
	}
}
